package com.lsz.deport.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lsz.deport.R;
import com.lsz.deport.json.OrderContentBean;
import com.util_code.widget.recycleadpter.BaseRecycleViewAdapter;

import butterknife.BindView;
import butterknife.ButterKnife;

public class OrderStatusAdapter extends BaseRecycleViewAdapter<OrderContentBean> {

    private OrderOnItemClickListener itemListener;

    public OrderStatusAdapter(Context context) {
        super(context);
    }

    public void setOnClickItemListener(OrderOnItemClickListener itemListener) {
        this.itemListener = itemListener;
    }

    @Override
    protected void onBindBaseViewHolder(RecyclerView.ViewHolder holder, OrderContentBean item) {
        if (holder instanceof OrderStatusHolder) {
            OrderStatusHolder statusHolder = (OrderStatusHolder) holder;
            statusHolder.tvReceiptName.setText("收货人：" + item.receiver);
            statusHolder.tvStoreName.setText(item.receiverAddr);

            if (TextUtils.equals(item.orderStatus, "0")) {
                statusHolder.tvStartBtn.setVisibility(View.VISIBLE);
                statusHolder.tvRestartBtn.setVisibility(View.GONE);
                statusHolder.tvCompleteBtn.setVisibility(View.GONE);
                statusHolder.tvReCompleteBtn.setVisibility(View.GONE);
            } else if (TextUtils.equals(item.orderStatus, "1")) {
                statusHolder.tvStartBtn.setVisibility(View.GONE);
                statusHolder.tvRestartBtn.setVisibility(View.VISIBLE);
                statusHolder.tvCompleteBtn.setVisibility(View.VISIBLE);
                statusHolder.tvReCompleteBtn.setVisibility(View.GONE);
            } else if (TextUtils.equals(item.orderStatus, "2")) {
                statusHolder.tvStartBtn.setVisibility(View.GONE);
                statusHolder.tvRestartBtn.setVisibility(View.GONE);
                statusHolder.tvCompleteBtn.setVisibility(View.GONE);
                statusHolder.tvReCompleteBtn.setVisibility(View.VISIBLE);
            }

            statusHolder.rlSongName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    callPhone(item.receiverPhone);
                }
            });
            setOnItemClick(holder.itemView, item, 0);//条目
            setOnItemClick(statusHolder.tvStartBtn, item, 1);//开始配送
            setOnItemClick(statusHolder.tvRestartBtn, item, 2);//恢复配送
            setOnItemClick(statusHolder.tvCompleteBtn, item, 3);//完成配送
            setOnItemClick(statusHolder.tvReCompleteBtn, item, 4);//恢复配送中
        }
    }

    private void setOnItemClick(View cView, OrderContentBean item, int type) {
        cView.setOnClickListener(view -> itemListener.onItemClick(view, item, type));
    }


    /**
     * 拨打电话（跳转到拨号界面，用户手动点击拨打）
     *
     * @param phoneNum 电话号码
     */
    private void callPhone(String phoneNum) {
        if (TextUtils.isEmpty(phoneNum)) return;
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        mContext.startActivity(intent);
    }

    @Override
    protected RecyclerView.ViewHolder onCreateBaseViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_order_multiple, parent, false);
        return new OrderStatusHolder(inflate);
    }

    @Override
    protected int getBaseItemViewType(int position) {
        return 0;
    }

    class OrderStatusHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.rl_song_name)
        RelativeLayout rlSongName;
        @BindView(R.id.tv_receipt_name)
        TextView tvReceiptName;
        @BindView(R.id.tv_store_name)
        TextView tvStoreName;
        @BindView(R.id.tv_address)
        TextView tvAddress;
        @BindView(R.id.tv_start_btn)
        TextView tvStartBtn;
        @BindView(R.id.tv_restart_btn)
        TextView tvRestartBtn;
        @BindView(R.id.tv_complete_btn)
        TextView tvCompleteBtn;
        @BindView(R.id.tv_re_complete_btn)
        TextView tvReCompleteBtn;

        OrderStatusHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    interface OrderOnItemClickListener {
        <T> void onItemClick(View view, T t, int type);
    }
}
